package com.hermes.action.json;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hermes.biz.common.model.comment.UserCommentDisplayUnitDO;
import com.hermes.biz.common.model.item.ItemDO;
import com.hermes.biz.common.model.result.AsynResultDO;
import com.hermes.biz.common.model.user.UserCommentDO;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.biz.manager.user.UserCommentManager;
import com.hermes.biz.service.item.ItemService;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.AsynBaseAction;
import com.hermes.common.util.ParameterParserUtil;




public class AsynGetItemToHtml extends AsynBaseAction {


	private static final long serialVersionUID = -2595566995217130413L;

	private Logger  log = Logger.getLogger(AsynGetItemToHtml.class);
	
	private ItemService itemService;

	private UserCommentManager userCommentManager ;
	
	private UserService userService;
	
	private List<AsynResultDO>  resultList;
	
	private final int numToDis=8;

	private String content;
	
	private String itemTitle;
	
	private String itemPic;
	
	public String execute() {
		long timebegin=System.currentTimeMillis();
		int nextPageInt=0;
		String nextPage = ParameterParserUtil.getParameter("next");
		if(StringUtil.isNotBlank(nextPage)){
			 nextPageInt = Integer.parseInt(nextPage)-1;
		}
		
		int tagId=1;
		String tag = ParameterParserUtil.getParameter("tag");
		if(StringUtil.isNotBlank(tag)){
			tagId = Integer.parseInt(tag);
		}
		
		List<ItemDO> result = itemService.getItemByItemTagId(tagId,nextPageInt*numToDis,nextPageInt*numToDis+numToDis);
		
		log.error("chuqudaxiao:"+result.size());
		
		int num2c=0;
		
		if (result.size()>0){
			resultList = new ArrayList<AsynResultDO>();
			for(ItemDO item: result){
				num2c++;
				AsynResultDO temp = new AsynResultDO();
				temp.setItemPic(item.getItemPicUrl());
				temp.setItemTitle(item.getItemTitle());
				temp.setDescription(item.getDescription());
				temp.setItemId(item.getItemId());
				temp.setTagId(item.getTagId());
				temp.setBeLovedNum(item.getBelovedNum());

				
				List<UserCommentDO> userComments = userCommentManager.getUserCommentInfoByItemIdV2(item.getItemId());
				
				if(userComments!=null && userComments.size()>0){

					for(UserCommentDO commentDO:userComments){

						UserCommentDisplayUnitDO commentUnit = new UserCommentDisplayUnitDO();
						commentUnit.setComment(commentDO.getComment());
						commentUnit.setCommentId(commentDO.getUserCommentId());
						commentUnit.setUserId(commentDO.getCreateUserId());
						commentUnit.setHeadPic(userService.getUserInfoByUserId(commentDO.getCreateUserId()).getModule().getHeadPic());
						commentUnit.setUserName(userService.getUserInfoByUserId(commentDO.getCreateUserId()).getModule().getUserName());
						temp.getComments().add(commentUnit);
					}

				}
				
				resultList.add(temp);
				if(num2c>20){
					break;
				}
			}
						
			setContent(String.valueOf(resultList.size()));
			
			long timeend=System.currentTimeMillis();
			log.error("*********index-json-time-consum-milsec*********:"+(timeend-timebegin));
			log.error("*********index-json-time-consum*********:"+(timeend-timebegin)/1000);

			return SUCCESS;
		}
			return ERROR;
}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public String getItemTitle() {
		return itemTitle;
	}

	public void setItemTitle(String itemTitle) {
		this.itemTitle = itemTitle;
	}

	public String getItemPic() {
		return itemPic;
	}

	public void setItemPic(String itemPic) {
		this.itemPic = itemPic;
	}

	public List<AsynResultDO> getResultList() {
		return resultList;
	}

	public void setResultList(List<AsynResultDO> resultList) {
		this.resultList = resultList;
	}
	public void setUserCommentManager (UserCommentManager  userCommentManager ){
		this.userCommentManager  = userCommentManager ;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
